package gang.org.springframework.framework.io;

import gang.org.springframework.framework.util.GangClassUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gang.chen
 * @Date 2021/8/4 12:20
 */
public class GangDefaultResourceLoader implements GangResourceLoader {

    private ClassLoader classLoader;

    private final Map<Class<?>, Map<GangResource, ?>> resourceCaches = new ConcurrentHashMap<>(4);

    @Override
    public ClassLoader getClassLoader() {
        return this.classLoader != null ? this.classLoader : GangClassUtils.getDefaultClassLoader();
    }

    @Override
    public GangResource getResource(String location) {

        //TODO
        return new GangClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()),getClassLoader());
    }

    public<T> Map<GangResource, T> getResourceCache(Class<T> valueType){
        return (Map<GangResource, T>) this.resourceCaches.computeIfAbsent(valueType, key -> new ConcurrentHashMap<>());
    }
}
